// store/index.js
import { createStore } from "vuex";

const store = createStore({
  state() {
    return {
      weatherData: null,
      location: null,
      error: null,
    };
  },
  mutations: {
    setWeatherData(state, data) {
      state.weatherData = data;
      // 存储当前城市到本地缓存
      uni.setStorageSync("currentCity", data.city);
    },
    setLocation(state, location) {
      state.location = location;
    },
    setError(state, error) {
      state.error = error;
    },
  },
  actions: {
    async fetchWeatherByLocation({ commit }, { latitude, longitude }) {
      // 实际项目中应该调用真实的天气API
      // 这里使用模拟数据
      return new Promise((resolve) => {
        setTimeout(() => {
          // 模拟API响应
          const mockData = {
            city: "北京",
            current: {
              temperature: 22,
              condition: "晴",
              weatherType: "sunny",
              humidity: 45,
              windSpeed: 3,
              uvIndex: 6,
              feelsLike: 23,
            },
            forecast: [
              { date: "周一", weatherType: "sunny", temperature: 24 },
              { date: "周二", weatherType: "cloudy", temperature: 22 },
              { date: "周三", weatherType: "rainy", temperature: 19 },
              { date: "周四", weatherType: "cloudy", temperature: 20 },
              { date: "周五", weatherType: "sunny", temperature: 23 },
              { date: "周六", weatherType: "sunny", temperature: 25 },
              { date: "周日", weatherType: "cloudy", temperature: 23 },
            ],
          };

          commit("setWeatherData", mockData);
          resolve(mockData);
        }, 1000);
      });
    },

    async fetchWeatherByCity({ commit }, city) {
      // 实际项目中应该调用真实的天气API
      // 这里使用模拟数据
      return new Promise((resolve) => {
        setTimeout(() => {
          // 模拟API响应
          const mockData = {
            city: city,
            current: {
              temperature: Math.floor(Math.random() * 40) - 5,
              condition: "晴",
              weatherType: "sunny",
              humidity: Math.floor(Math.random() * 100),
              windSpeed: Math.floor(Math.random() * 30),
              uvIndex: Math.floor(Math.random() * 11),
              feelsLike: Math.floor(Math.random() * 40) - 5,
            },
            forecast: [
              {
                date: "周一",
                weatherType: "sunny",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周二",
                weatherType: "cloudy",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周三",
                weatherType: "rainy",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周四",
                weatherType: "cloudy",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周五",
                weatherType: "sunny",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周六",
                weatherType: "sunny",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
              {
                date: "周日",
                weatherType: "cloudy",
                temperature: Math.floor(Math.random() * 40) - 5,
              },
            ],
          };

          commit("setWeatherData", mockData);
          resolve(mockData);
        }, 1000);
      });
    },
  },
  getters: {
    currentWeather(state) {
      return state.weatherData?.current;
    },
    forecast(state) {
      return state.weatherData?.forecast || [];
    },
    currentCity(state) {
      return state.weatherData?.city || "未知城市";
    },
  },
});

export default store;
